package com.yz.crm.passport.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yz.crm.company.service.CompanyService;
import com.yz.crm.db.model.BaseCompany;
import com.yz.crm.db.model.BaseEmployee;
import com.yz.crm.employee.service.EmployeeService;
import com.yz.crm.passport.model.SessionUser;
import com.yz.crm.passport.service.PassportService;
import com.yz.crm.system.service.RoleService;
/**
 * 
 * */
@Service
public class PassportServiceImpl implements PassportService {

	@Resource
	private CompanyService companyService;
	
	@Resource
	private EmployeeService employeeService;
	
	@Resource
	private RoleService roleService;
	
	@Override
	public SessionUser login(String username, String password) {
		
		BaseEmployee emp = employeeLogin(username, password);
		if (emp==null){
			return null;
		}
		
		return fill(emp);
	}
	
	@Override
	public SessionUser login(String employeeId) {
		
		BaseEmployee emp = employeeService.get(employeeId);
		if (emp==null){
			return null;
		}
		
		return fill(emp);
	}
	

	/**
	 * 
	 * */
	private SessionUser fill(BaseEmployee employee){
		
		SessionUser user = new SessionUser();
		
		user.setEmployee(employee);
		
		BaseCompany company = companyService.get(employee.getCompanyId());
		user.setCompany(company);
		
		user.setRoleList(roleService.getRoles(employee.getId()));
		
		return user;
	}
	
	/**
	 * 
	 * */
	private BaseEmployee employeeLogin(String username, String password){
		
		
		BaseEmployee employee = employeeService.getByUsername(username, password);
		
		return employee;
	}
}
